package com.agriculture.controller;

import com.agriculture.common.R;
import com.agriculture.entity.Carousel;
import com.agriculture.entity.Category;
import com.agriculture.entity.Good;
import com.agriculture.service.CarouselService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图操作
 */
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("Carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @GetMapping("list")
    public R<List<Carousel>> getAll(){
        //条件构造器
        LambdaQueryWrapper<Carousel> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(Carousel::getIsDeleted,0);
        List<Carousel> list = carouselService.list(queryWrapper);
        return R.success(list);
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        //分页构造器
        Page<Carousel> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Carousel> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
//        queryWrapper.orderByAsc(Category::getSort);
        queryWrapper.eq(Carousel::getIsDeleted,0);
        //分页查询
        carouselService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @PostMapping
    public R<String> addNew(@RequestBody Carousel carousel){
        carouselService.save(carousel);
        return R.success("新增成功！");
    }

    @DeleteMapping
    public R<String> delCarousel(@RequestBody Carousel carousel){
        carouselService.removeById(carousel);
        return R.success("删除成功!");
    }

    @PutMapping
    public R<String> upCarousel(@RequestBody Carousel carousel){
        carouselService.updateById(carousel);
        return R.success("修改成功！");
    }
}






















